package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishSetmealDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Consumer;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public void saveSetmeal(SetmealDTO setmealDTO) {
        //新增套餐
        Setmeal setmeal = new Setmeal();
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.saveSetmeal(setmeal);
        //新增菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(!CollectionUtils.isEmpty(setmealDishes)){
            setmealDishes.forEach(new Consumer<SetmealDish>() {
                @Override
                public void accept(SetmealDish setmealDish) {
                    setmealDish.setSetmealId(setmeal.getId());
                }
            });
        }
        setmealDishMapper.saveDish(setmealDishes);

    }

    @Override
    public void deleteSetmeal(List<Integer> ids) {
        //删除套餐
        setmealMapper.deleteSetmeal(ids);
        //删除套餐中间表
        setmealDishMapper.deleteSetmealDish(ids);
    }

    @Override
    public void putSetmeal(SetmealDTO setmealDTO) {
        //修改套餐表
        Setmeal setmeal = new Setmeal();
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.putSetmeal(setmeal);
        //修改套餐中间表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.putSetmealDishe(setmealDishes);
    }

    //条件分页查询
    @Override
    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
       Page<SetmealVO> setmealPage= setmealMapper.pageSetmeal(setmealPageQueryDTO.getCategoryId(),
                setmealPageQueryDTO.getName(),
                setmealPageQueryDTO.getStatus());
        return new PageResult(setmealPage.getTotal(),setmealPage.getResult());
    }

    //启售 停售
    @Override
    public void setmealStatus(Integer status, Integer id) {
        setmealMapper.setmealStatus(status,id);
    }

    @Override
    public SetmealDTO selectById(Long id) {
        //查询套餐
        SetmealDTO setmealDTO = new SetmealDTO();
        Setmeal setmeal= setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal,setmealDTO);
        //查询中间表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        DishSetmealDTO dishSetmealDTO= setmealDishMapper.selectSetmealDish(setmeal.getId());
        BeanUtils.copyProperties(dishSetmealDTO,setmealDishes);
        return setmealDTO;
    }
}
